package com.rbcs.sharing.webapp;

import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;

import com.rbcs.base.item.service.ItemManager;
import com.rbcs.base.model.Item;
import com.rbcs.sharing.model.Sharing;
import com.rbcs.sharing.model.SharingItem;
import com.rbcs.sharing.service.SharingManager;
import com.systop.common.modules.dept.model.Dept;
import com.systop.common.modules.dept.service.DeptManager;
import com.systop.common.modules.security.user.UserUtil;
import com.systop.core.webapp.struts2.action.DefaultCrudAction;

@SuppressWarnings("serial")
@Controller
@Scope(BeanDefinition.SCOPE_PROTOTYPE)
public class SharingAction extends DefaultCrudAction<Sharing, SharingManager> {

	private Integer[] sharingItemIds;

	@Autowired
	private DeptManager deptManager;

	@Autowired
	private ItemManager itemManager;

	public String index() {
		String hql = "from Sharing s order by s.year";
		items = getManager().query(hql);
		return INDEX;
	}

	/**
	 * 创建公用费用分摊
	 */
	@Transactional
	public String editNew() {
		Sharing sharing = new Sharing();
		Integer year = Calendar.getInstance().get(Calendar.YEAR);
		sharing.setYear(year);
		if (getManager().getDao().exists(sharing, "year")) {
			addActionError(year + "年度费用分摊已经存在");
			return INPUT;
		}
		sharing.setCode("FT-" + year);
		sharing.setCreater(UserUtil.getLoginUser(getRequest()));
		getManager().save(sharing);
		List<Item> items = itemManager.getIsShareItems();
		List<Dept> depts = deptManager.getIsLeafDepts();
		for (Dept dept : depts) {
			for (Item item : items) {
				SharingItem sitem = new SharingItem();
				sitem.setAmount(0.0);
				sitem.setDept(dept);
				sitem.setItem(item);
				sitem.setSharing(sharing);
				getManager().getDao().save(sitem);
			}
		}
		setModel(sharing);
		return "createSuccess";
	}

	/**
	 * 编辑更新公共费用分摊
	 */
	@Transactional
	public String update() {
		for (Integer sitemId : sharingItemIds) {
			SharingItem sitem = getSharingItem(sitemId);
			if (sitem != null) {//判断分摊项不为空
				sitem.setAmount(getDoubleValue("amount_", sitemId));
				getManager().getDao().save(sitem);
			}
		}
		getManager().countYearSharing(getModel());
		addActionMessage("分摊费用保存成功");
		return INPUT;
	}
	
	public String remove(){
		getManager().remove(getModel());
		return SUCCESS;
	}

	// 获得参数值，并转换成Double类型
	private Double getDoubleValue(String parmarPre, Integer id) {
		String doubleValue = getRequest().getParameter(parmarPre + id);
		if (StringUtils.isNotBlank(doubleValue)) {
			return Double.valueOf(doubleValue);
		}
		return 0.0;
	}

	private SharingItem getSharingItem(Integer id) {
		return getManager().getDao().get(SharingItem.class, id);
	}

	public void setSharingItemIds(Integer[] sharingItemIds) {
		this.sharingItemIds = sharingItemIds;
	}
}
